//
//  CoilViewController.m
//  项目三
//
//  Created by rimi1 on 2016/12/14.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "CoilViewController.h"
#import "ConcernViewController.h"
#import "SignInViewController.h"
#import "SearchUserViewController.h"
#import "AboutWeViewController.h"
#import "FeedbackViewController.h"
#import "PersonalViewController.h"

#import "ClearCache.h"// 清除缓存

@interface CoilViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <NSMutableArray *> *dataArr;

@end

@implementation CoilViewController

- (void)viewWillAppear:(BOOL)animated {
    [self getCacheSize];
    [self initHeadImg];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeAppearance];
}

#pragma mark - initialize

- (void)initializeDataSource {
    self.dataArr = [NSMutableArray array];
    
    NSArray *nameArr = @[@[@"关注动态", @"寻找大厨"], @[@"联系我们", @"意见反馈", @"清除缓存"]];
    for (int i = 0; i < nameArr.count; i++) {
        NSArray *arr = nameArr[i];
        NSMutableArray *muArr = [NSMutableArray array];
        
        for (int j = 0; j < arr.count; j++) {
            NSMutableDictionary *dic = [@{@"title":arr[j], @"subTitle":@""} mutableCopy];
            [muArr addObject:dic];
        }
        
        [self.dataArr addObject:muArr];
    }
}

- (void)initializeAppearance {
    self.navigationBar.left.frame = CGRectMake(10, 25, 34, 34);
    self.navigationBar.left.layer.cornerRadius = self.navigationBar.left.bounds.size.width/2;
    self.navigationBar.left.layer.masksToBounds = YES;
    self.navigationBar.title.text = @"美食圈";
    [self.view addSubview:self.tableView];
}

- (void)initHeadImg {
    NSURL *url = [NSURL URLWithString:[MyUser sharedUser].headImage];
    UIImage *img = [UIImage imageNamed:@"个人"];
    [self.navigationBar.left.imageView sd_setImageWithURL:url placeholderImage:img completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (image) {
            [self.navigationBar.left setImage:image forState:UIControlStateNormal];
        }else {
            [self.navigationBar.left setImage:img forState:UIControlStateNormal];
        }
    }];
}

#pragma mark - respondsTo...

- (void)respondsToLeft:(UIButton *)sender {
    PersonalViewController *personal = [[PersonalViewController alloc] init];
    personal.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:personal animated:YES];
}


#pragma mark - Custom Method
// 获取缓存大小
- (void)getCacheSize {
    NSString *cacheSize = [ClearCache getCacheSize];
    self.dataArr[1][2][@"subTitle"] = cacheSize;
    [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:2 inSection:1]] withRowAnimation:UITableViewRowAnimationFade];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *arr = self.dataArr[section];
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *resuing = @"CoilCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:resuing];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:resuing];
    }
    NSDictionary *dic = self.dataArr[indexPath.section][indexPath.row];
    
    cell.imageView.image = [UIImage imageNamed:dic[@"title"]];
    cell.textLabel.text = dic[@"title"];
    cell.detailTextLabel.text = dic[@"subTitle"];
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    // 第一层分区，  第二层，该分区的行
    switch (indexPath.section) {
        case 0:{
            ConcernViewController *concern = [[ConcernViewController alloc] init];
            concern.hidesBottomBarWhenPushed = YES;
            SignInViewController *sign = [[SignInViewController alloc] init];
            sign.hidesBottomBarWhenPushed = YES;
            SearchUserViewController *search = [[SearchUserViewController alloc] init];
            search.hidesBottomBarWhenPushed = YES;
            
            NSArray <UIViewController *> *arr = @[concern, search, sign];
            switch (indexPath.row) {
                case 0:{
                    if ([MyUser sharedUser].isSignIn) {
                        [self.navigationController pushViewController:arr[0] animated:YES];
                    }else {
                        [self.navigationController pushViewController:arr[2] animated:YES];
                    }
                }
                    break;
                    
                case 1:{
                    [self.navigationController pushViewController:arr[1] animated:YES];
                }
                    break;
                    
                default:
                    break;
            }
        }
            break;
            
        case 1:{
            AboutWeViewController *about = [[AboutWeViewController alloc] init];
            about.hidesBottomBarWhenPushed = YES;
            FeedbackViewController *feed = [[FeedbackViewController alloc] init];
            feed.hidesBottomBarWhenPushed = YES;
            NSArray *arr = @[about, feed];
            
            if (indexPath.row != 2) {
                [self.navigationController pushViewController:arr[indexPath.row] animated:YES];
            }else {
                [Alert alertCancelWithTitle:@"是否要清除缓存？" message:nil sure:^{
                    BOOL is = [ClearCache clearCache];
                    if (!is) {
                        [Alert alertWithTitle:@"清除缓存失败" message:nil];
                    }else {
                        [self getCacheSize];
                    }
                }];
            }
        }
            break;
            
        default:
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

#pragma mark - get

- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kWIDTH, kHEIGHT - 64) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}


@end










